<?php

namespace App\HttpController\XAppWxapp\WxappXK\Admin;


use App\Common\Log;
use App\Common\OA\OA;
use App\HttpController\ControllerAdmin;
use App\Model\t_s_app;
use App\Model\t_wx_reply;
use App\Model\t_wx_user_a_xf;
use App\Traits\TraitExcel;
use XWX\Common\H;
use XWX\Common\Validate\Validate;

class User extends ControllerAdmin
{
    use TraitExcel;


    public function index()
    {
        $this->funcSetTitle('用户列表');


        $compKw = $this->reqParam('compKw');
        $compPhone = $this->reqParam('compPhone');


        $query = $this->db()
            ->whereIn('wxuserxf_x_id', $this->funcGetX())
            ->orderBy('wxuserxf_create_date');

        if (H::funcStrHasAnyText($compKw))
        {
            $query->where('wxuserxf_name', "%{$compKw}%", 'LIKE');
        }
        if (H::funcStrHasAnyText($compPhone))
        {
            $query->where('wxuserxf_phone', $compPhone);
        }


        if (!$this->funcIsAdminExport())
        {
            $query_count = clone $query;

            $list = $query->get(t_wx_user_a_xf::table(), [$this->funcGetPageIndexXPage(), $this->funcGetPageSize()]);
            $count = $query_count->count(t_wx_user_a_xf::table());


            //二次加工
            $list_ = [];
            foreach ($list as $v)
            {
                $data = $v;

                $data['id'] = $v['wxuserxf_id'];

                $list_[] = $data;
            }


            $this->commonR()->setData("list", $list_);
            $this->commonR()->setData("count", $count);


            $this->funcWriteCommonR();
        }
        else
        {
            //导出
            $table_head = [
                'wxuserxf_name' => '姓名',
                'phone' => '手机号',
                'wxuserxf_school' => '学校',
                'wxuserxf_subject' => '科目',
                'wxuserxf_teacher_age' => '教学年龄',
            ];


            $this->funcExport($query, t_wx_user_a_xf::table(), $table_head, function ($row)
            {
                //追加判断
                $row['phone'] = "`{$row['wxuserxf_phone']}";

                return $row;
            });
        }
    }


    /** 导出 */
    public function export()
    {
        $this->index();
    }
}